package com.battle.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.TrainingGroup;
import com.battle.pojo.entity.TrainingProgram;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.trainingGroup.*;
import com.battle.pojo.vo.trainingGroup.*;
import com.battle.pojo.dto.trainingGroup.*;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.TrainingGroupRepository;
import com.battle.service.repository.in.TrainingProgramRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.TrainingGroupService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：训练项目服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TrainingGroupServiceImpl implements TrainingGroupService {

    private final TrainingGroupRepository trainingGroupRepository;
    private final UserRepository userRepository;
    private final OrganizationRepository organizationRepository;
    private final TrainingProgramRepository programRepository;

    public TrainingGroupServiceImpl(TrainingGroupRepository trainingGroupRepository, UserRepository userRepository, OrganizationRepository organizationRepository, TrainingProgramRepository programRepository) {
        this.trainingGroupRepository = trainingGroupRepository;
        this.userRepository = userRepository;
        this.organizationRepository = organizationRepository;
        this.programRepository = programRepository;
    }


    /**
     * 创建小组训练
     *
     * @param param
     */
    @Override
    public void createTrainingGroup(CreateTrainingGroupParam param) {
        TrainingGroup trainingGroup = new TrainingGroup();
        BeanUtils.copyProperties(param, trainingGroup);
        Long id = IdGenerator.SNOW.generate();
        trainingGroup.setId(id);
        if ( param.getTrainingPerformance()!=null){
            trainingGroup.setTrainingNumber(param.getTrainingPerformance().size());
        }

        TrainingProgram program = programRepository.getByIdCheck(param.getTrainingProgramId());
        trainingGroup.setType(program.getType());
        trainingGroup.setUnit(program.getUnit());
        String training = JSONArray.toJSONString(param.getTrainingPerformance());
        trainingGroup.setTrainingPerformance(training);

        trainingGroupRepository.create(trainingGroup);
    }

    /**
     * 修改小组训练
     *
     * @param param
     */
    @Override
    public void editTrainingGroup(EditTrainingGroupParam param) {
        Long id = param.getId();
        TrainingGroup trainingGroup = trainingGroupRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, trainingGroup);
        if ( param.getTrainingPerformance()!=null){
            trainingGroup.setTrainingNumber(param.getTrainingPerformance().size());
        }

        TrainingProgram program = programRepository.getByIdCheck(param.getTrainingProgramId());
        trainingGroup.setType(program.getType());
        trainingGroup.setUnit(program.getUnit());

        String training = JSONArray.toJSONString(param.getTrainingPerformance());
        trainingGroup.setTrainingPerformance(training);
        trainingGroupRepository.edit(trainingGroup);
    }

    /**
     * 移除小组训练
     *
     * @param id
     */
    @Override
    public void removeTrainingGroup(Long id) {
        trainingGroupRepository.removeById(id);
    }

    /**
     * 启动/禁用小组训练
     *
     * @param id
     */
    @Override
    public void enabledTrainingGroup(Long id) {
        TrainingGroup trainingGroup = trainingGroupRepository.getByIdCheck(id);
        trainingGroup.setIsDisable(!trainingGroup.getIsDisable());
        trainingGroupRepository.edit(trainingGroup);
    }

    /**
     * 获取小组训练详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTrainingGroupVo getTrainingGroup(Long id) {
        TrainingGroup trainingGroup = trainingGroupRepository.getByIdCheck(id);
        GetTrainingGroupVo vo = new GetTrainingGroupVo();
        BeanUtils.copyProperties(trainingGroup, vo);

        List<TrainingPerformanceParam> trainingPerformanceParams = JSONArray.parseArray(trainingGroup.getTrainingPerformance(), TrainingPerformanceParam.class);
        if (trainingPerformanceParams != null) {
            for (TrainingPerformanceParam param : trainingPerformanceParams) {
                User user = userRepository.getById(param.getUserId());
                if (user != null) {
                    param.setUserName(user.getName());
                }
            }
        }
        Organization organization = organizationRepository.getById(trainingGroup.getDepartmentId());
        if (organization!=null){
            vo.setOrganizationName(organization.getName());
        }

        User createUser = userRepository.getById(trainingGroup.getCreator());
        if (createUser != null) {
            vo.setCreateName(createUser.getName());
        }
        vo.setCreateTime(trainingGroup.getCreateTime());
        vo.setTrainingPerformance(trainingPerformanceParams);
        return vo;
    }

    /**
     * 获取小组训练 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTrainingGroupListVo> getTrainingGroupList(GetTrainingGroupListParam param) {

        SelectTrainingGroupDto dto = new SelectTrainingGroupDto();
        BeanUtils.copyProperties(param, dto);
        if (param.getOrganizationId() != null) {
            List<Long> ids = organizationRepository.getOrganizationIdList(param.getOrganizationId());
            dto.setOrganizationIds(ids);
        }
        Page<GetTrainingGroupListVo> page = trainingGroupRepository.selectTrainingGroup(dto);
        return new PageResult<>(page.getRecords(), page);

    }

    /**
     * 获取小组训练下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTrainingGroupDownListVo> getTrainingGroupDownList(GetTrainingGroupDownListParam param) {
        TrainingGroup po = new TrainingGroup();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TrainingGroup> trainingGroupList = trainingGroupRepository.getListByEntity(po);

        List<GetTrainingGroupDownListVo> list = new ArrayList<>();
        for (TrainingGroup trainingGroup : trainingGroupList) {
            GetTrainingGroupDownListVo vo = new GetTrainingGroupDownListVo();
            BeanUtils.copyProperties(trainingGroup, vo);
            list.add(vo);
        }
        return list;
    }


}



